﻿using System;
using System.Data;
using BLL;
using Ext.Net;
using Roles;

namespace ERP.Modules
{
    public partial class Customer : System.Web.UI.UserControl
    {
        private readonly Customer_BLL clsCustomer = new Customer_BLL();
        private readonly Customer_Type_BLL _clsCustomerType = new Customer_Type_BLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                //BuildTree(TreePanel1.Root);
                GetCustomerType();
                GetData();
                GetComboBox();
                ClearTextBox();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
                WindowsCustomer_Type();
                WindowsSector();
                WindowsEnterprise();
                WindowsProvince();
                WindowsDistricts();
                WindowsBank();
            }
        }
        //private void BuildTree(NodeCollection nodes)
        //{
        //    if (nodes == null)
        //    {
        //        nodes = new NodeCollection();
        //    }
        //    var root = new Node { Text = "Root" };
        //    nodes.Add(root);
        //    var dtCustomer = clsCustomer.BindGirdCustomer(string.Empty);
        //    for (var i = 0; i < dtCustomer.Rows.Count; i++)
        //    {
        //        var node = new Node
        //        {
        //            NodeID = dtCustomer.Rows[i]["Customer_ID"].ToString(),
        //            Text = dtCustomer.Rows[i]["Customer_Name"].ToString(),
        //            Leaf = true
        //        };
        //        root.Children.Add(node);
        //    }
        //}
        //-------Store-Datasource-----------------
        protected void StoreCustomer_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            SelectBoxCustomerCustomerType.Text = string.Empty;
            GetData();
            GetCustomerType();
        }
        //--------Buttom-Main------------------------
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            GetComboBox();
            ClearTextBox();
            WindowsCustomer.Show();
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grCustomer.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    GetComboBox();
                    ClearTextBox();
                    DataDetail(int.Parse(row.RecordID));
                    WindowsCustomer.Show();
                    sm.UpdateSelection();
                }
        }
        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grCustomer.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    try
                    {
                        Customer_BLL.Deletetb_Customer(int.Parse(row.RecordID));
                    }
                    catch (Exception)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                    }
                }
            GetData();
        }
        protected void SelectBoxCustomerCustomerType_Change(object sender, DirectEventArgs e)
        {
            GetData();
        }
        //--------Buttom-Popup------------------------
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            if (txtCustomer_ID.Text == string.Empty)
            {
                InsertCustomer();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Bạn không được cấp quyền để sửa đổi !");
                }
                else
                {
                    UpdateCustomer();
                }
            }
        }
        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            WindowsCustomer.Close();
        }
        //----------Click-------------------
        protected void CustomerDblClick(object sender, DirectEventArgs e)
        {
            GetComboBox();
            ClearTextBox();
            int Customer_ID = int.Parse(e.ExtraParams["Customer_ID"]);
            DataDetail(Customer_ID);
            WindowsCustomer.Show();
        }
        //----------Button-Windows---------------
        #region[Method]
        protected void GetData()
        {
            if (String.IsNullOrEmpty(SelectBoxCustomerCustomerType.Text))
            {
                DataTable dtCustomer = clsCustomer.BindGirdtb_Customer(string.Empty);
                if (dtCustomer != null && dtCustomer.Rows.Count > 0)
                {
                    StoreCustomer.DataSource = dtCustomer;
                    StoreCustomer.DataBind();
                }
                else
                {
                    grCustomer.GetStore().RemoveAll();
                }
            }
            else
            {
                DataTable dtCustomer =
                    clsCustomer.GetCustomerFillter(int.Parse(SelectBoxCustomerCustomerType.SelectedItem.Value));
                if (dtCustomer != null && dtCustomer.Rows.Count > 0)
                {
                    StoreCustomer.DataSource = dtCustomer;
                    StoreCustomer.DataBind();
                }
                else
                {
                    grCustomer.GetStore().RemoveAll();
                }
            }
        }
        protected void DataDetail(int id)
        {
            DataTable dtCustomer = clsCustomer.GetCustomerDetail(id);
            if (dtCustomer != null && dtCustomer.Rows.Count > 0)
            {
                txtCustomer_ID.Text = dtCustomer.Rows[0]["Customer_ID"].ToString();
                txtCustomer_Alias.Text = dtCustomer.Rows[0]["Customer_Alias"].ToString();
                cbCustomer_Type.SetValue(int.Parse(dtCustomer.Rows[0]["Customer_Type_ID"].ToString()));
                cbEnterprise.SetValue(int.Parse(dtCustomer.Rows[0]["Enterprise_ID"].ToString()));
                txtCustomer_Name.Text = dtCustomer.Rows[0]["Customer_Name"].ToString();
                txtCustomer_Shortcut.Text = dtCustomer.Rows[0]["Customer_Shortcut"].ToString();
                txtCustomer_Name_EN.Text = dtCustomer.Rows[0]["Customer_Name_EN"].ToString();
                txtCustomer_Name_Exchange.Text = dtCustomer.Rows[0]["Customer_Name_Exchange"].ToString();
                txtCustomer_Debt_Limit.Text = dtCustomer.Rows[0]["Customer_Debt_Limit"].ToString();
                txtCustomer_Address.Text = dtCustomer.Rows[0]["Customer_Address"].ToString();
                txtCustomer_Payment_Deadline.Text = dtCustomer.Rows[0]["Customer_Payment_Deadline"].ToString();
                cbDistricts.SetValue(int.Parse(dtCustomer.Rows[0]["District_ID"].ToString()));
                txtCustomer_Establish.Text = dtCustomer.Rows[0]["Customer_Establish"].ToString();
                txtCustomer_Phone1.Text = dtCustomer.Rows[0]["Customer_Phone1"].ToString();
                txtCustomer_Phone2.Text = dtCustomer.Rows[0]["Customer_Phone2"].ToString();
                txtCustomer_Fax.Text = dtCustomer.Rows[0]["Customer_Fax"].ToString();
                txtCustomer_Website.Text = dtCustomer.Rows[0]["Customer_Website"].ToString();
                cbSector.SetValue(int.Parse(dtCustomer.Rows[0]["Sector_ID"].ToString()));
                txtCustomer_Account_Number.Text = dtCustomer.Rows[0]["Customer_Account_Number"].ToString();
                txtCustomer_Contact_ID.Text = dtCustomer.Rows[0]["Customer_Contact"].ToString();
                txtCustomer_Contact_Name.Text = dtCustomer.Rows[0]["Contact_Name"].ToString();
                cbBank.SetValue(int.Parse(dtCustomer.Rows[0]["Bank_ID"].ToString()));
                txtCustomer_Contract_FullName.Text = dtCustomer.Rows[0]["Customer_Contract_FullName"].ToString();
                txtCustomer_Contract_Address.Text = dtCustomer.Rows[0]["Customer_Contract_Address"].ToString();
                cbDistricts1.SetValue(int.Parse(dtCustomer.Rows[0]["Customer_Contract_District"].ToString()));
                txtCustomer_Contract_Tax_Code.Text = dtCustomer.Rows[0]["Customer_Contract_Tax_Code"].ToString();
                txtCustomer_Contract_Contact_ID.Text = dtCustomer.Rows[0]["Customer_Contract_Contact"].ToString();
                txtCustomer_Contract_Contact_Name.Text = dtCustomer.Rows[0]["Contract_Contact_Name"].ToString();
                txtCustomer_Delivery_Address.Text = dtCustomer.Rows[0]["Customer_Delivery_Address"].ToString();
                txtCustomer_Delivery_Contact_ID.Text = dtCustomer.Rows[0]["Customer_Delivery_Contact"].ToString();
                txtCustomer_Delivery_Contact_Name.Text = dtCustomer.Rows[0]["Contract_Delivery_Name"].ToString();
                txtCustomer_Payment_Address.Text = dtCustomer.Rows[0]["Customer_Payment_Address"].ToString();
                txtCustomer_Payment_Contact_ID.Text = dtCustomer.Rows[0]["Customer_Payment_Contact"].ToString();
                txtCustomer_Payment_Contact_Name.Text = dtCustomer.Rows[0]["Contract_Payment_Name"].ToString();
                txtCustomer_Note.Text = dtCustomer.Rows[0]["Customer_Note"].ToString();
            }
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var Customer = new Customer_BLL();
            var dtCustomer = Customer.BindGirdtb_Customer(string.Empty);
            if (dtCustomer != null && dtCustomer.Rows.Count > 0)
            {
                foreach (DataRow dr in dtCustomer.Rows)
                {
                    if (txtCustomer_ID.Text != dr["Customer_ID"].ToString())
                    {
                        if (txtCustomer_Alias.Text == dr["Customer_Alias"].ToString())
                        {
                            kiemtra = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Mã khách hàng đã tồn tại !");
                        }
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertCustomer()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new ET.Customer_ET
                    {
                        Customer_Alias = txtCustomer_Alias.Text,
                        Customer_Type_ID = int.Parse(cbCustomer_Type.SelectedItem.Value),
                        Customer_Shortcut = txtCustomer_Shortcut.Text,
                        Customer_Name = txtCustomer_Name.Text,
                        Customer_Name_EN = txtCustomer_Name_EN.Text,
                        Customer_Name_Exchange = txtCustomer_Name_Exchange.Text,
                        Enterprise_ID = int.Parse(cbEnterprise.SelectedItem.Value),
                        Customer_Debt_Limit = float.Parse(txtCustomer_Debt_Limit.Text),
                        Customer_Address = txtCustomer_Address.Text,
                        Customer_Payment_Deadline = int.Parse(txtCustomer_Payment_Deadline.Text),
                        District_ID = int.Parse(cbDistricts.SelectedItem.Value),
                        Customer_Establish = DateTime.Parse(txtCustomer_Establish.Text),
                        Customer_Phone1 = txtCustomer_Phone1.Text,
                        Customer_Phone2 = txtCustomer_Phone2.Text,
                        Customer_Fax = txtCustomer_Fax.Text,
                        Customer_Website = txtCustomer_Website.Text,
                        Sector_ID = int.Parse(cbSector.SelectedItem.Value),
                        Customer_Account_Number = txtCustomer_Account_Number.Text,
                        Customer_Contact = int.Parse(txtCustomer_Contact_ID.Text),
                        Bank_ID = int.Parse(cbBank.SelectedItem.Value),
                        Customer_Contract_FullName = ckContract.Checked ? txtCustomer_Contract_FullName.Text : "",
                        Customer_Contract_Address = ckContract.Checked ? txtCustomer_Contract_Address.Text : "",
                        Customer_Contract_District = ckContract.Checked ? int.Parse(cbDistricts1.SelectedItem.Value) : int.Parse(cbDistricts.SelectedItem.Value),
                        Customer_Contract_Tax_Code = ckContract.Checked ? txtCustomer_Contract_Tax_Code.Text : "",
                        Customer_Contract_Contact = ckContract.Checked ? int.Parse(txtCustomer_Contract_Contact_ID.Text) : int.Parse(txtCustomer_Contact_ID.Text),
                        Customer_Delivery_Address = ckDelivery.Checked ? txtCustomer_Delivery_Address.Text : "",
                        Customer_Delivery_Contact = ckDelivery.Checked ? int.Parse(txtCustomer_Delivery_Contact_ID.Text) : int.Parse(txtCustomer_Contact_ID.Text),
                        Customer_Payment_Address = ckPayment.Checked ? txtCustomer_Payment_Address.Text : "",
                        Customer_Payment_Contact = ckPayment.Checked ? int.Parse(txtCustomer_Payment_Contact_ID.Text) : int.Parse(txtCustomer_Contact_ID.Text),
                        Customer_Note = ckPayment.Checked ? txtCustomer_Note.Text : "",
                        Customer_Date_Add = DateTime.Now,
                        Customer_Computer_Add = Environment.MachineName
                    };
                    Customer_BLL.Inserttb_Customer(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                    GetData();
                    GetComboBox();
                    ClearTextBox();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }
        protected void UpdateCustomer()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new ET.Customer_ET
                    {
                        Customer_ID = int.Parse(txtCustomer_ID.Text),
                        Customer_Alias = txtCustomer_Alias.Text,
                        Customer_Type_ID = int.Parse(cbCustomer_Type.SelectedItem.Value),
                        Customer_Shortcut = txtCustomer_Shortcut.Text,
                        Customer_Name = txtCustomer_Name.Text,
                        Customer_Name_EN = txtCustomer_Name_EN.Text,
                        Customer_Name_Exchange = txtCustomer_Name_Exchange.Text,
                        Enterprise_ID = int.Parse(cbEnterprise.SelectedItem.Value),
                        Customer_Debt_Limit = float.Parse(txtCustomer_Debt_Limit.Text),
                        Customer_Address = txtCustomer_Address.Text,
                        Customer_Payment_Deadline = int.Parse(txtCustomer_Payment_Deadline.Text),
                        District_ID = int.Parse(cbDistricts.SelectedItem.Value),
                        Customer_Establish = DateTime.Parse(txtCustomer_Establish.Text),
                        Customer_Phone1 = txtCustomer_Phone1.Text,
                        Customer_Phone2 = txtCustomer_Phone2.Text,
                        Customer_Fax = txtCustomer_Fax.Text,
                        Customer_Website = txtCustomer_Website.Text,
                        Sector_ID = int.Parse(cbSector.SelectedItem.Value),
                        Customer_Account_Number = txtCustomer_Account_Number.Text,
                        Customer_Contact = int.Parse(txtCustomer_Contact_ID.Text),
                        Bank_ID = int.Parse(cbBank.SelectedItem.Value),
                        Customer_Contract_FullName = ckContract.Checked ? txtCustomer_Contract_FullName.Text : "",
                        Customer_Contract_Address = ckContract.Checked ? txtCustomer_Contract_Address.Text : "",
                        Customer_Contract_District = ckContract.Checked ? int.Parse(cbDistricts1.SelectedItem.Value) : int.Parse(cbDistricts.SelectedItem.Value),
                        Customer_Contract_Tax_Code = ckContract.Checked ? txtCustomer_Contract_Tax_Code.Text : "",
                        Customer_Contract_Contact = ckContract.Checked ? int.Parse(txtCustomer_Contract_Contact_ID.Text) : int.Parse(txtCustomer_Contact_ID.Text),
                        Customer_Delivery_Address = ckDelivery.Checked ? txtCustomer_Delivery_Address.Text : "",
                        Customer_Delivery_Contact = ckDelivery.Checked ? int.Parse(txtCustomer_Delivery_Contact_ID.Text) : int.Parse(txtCustomer_Contact_ID.Text),
                        Customer_Payment_Address = ckPayment.Checked ? txtCustomer_Payment_Address.Text : "",
                        Customer_Payment_Contact = ckPayment.Checked ? int.Parse(txtCustomer_Payment_Contact_ID.Text) : int.Parse(txtCustomer_Contact_ID.Text),
                        Customer_Note = ckPayment.Checked ? txtCustomer_Note.Text : "",
                        Customer_Date_Modify = DateTime.Now,
                        Customer_Computer_Modify = Environment.MachineName
                    };
                    Customer_BLL.Updatetb_Customer(obj);
                    WindowsCustomer.Close();
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Sữa thành công!");
                    GetData();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }
        private void GetCustomerType()
        {
            var _dtCustomertype = _clsCustomerType.BindGirdtb_Customer_Type(string.Empty);
            if (_dtCustomertype != null && _dtCustomertype.Rows.Count > 0)
            {
                StoreCustomerCustomerType.DataSource = _dtCustomertype;
                StoreCustomerCustomerType.DataBind();
            }
            else
            {
                SelectBoxCustomerCustomerType.GetStore().RemoveAll();
            }
        }
        #endregion
        #region [ComboBox]
        protected void GetComboBox()
        {
            var clsCustomer_Type = new Customer_Type_BLL();
            DataTable dtCustomer_Type = clsCustomer_Type.BindGirdtb_Customer_Type(string.Empty);
            if (dtCustomer_Type != null && dtCustomer_Type.Rows.Count > 0)
            {
                Customer_TypeStore.DataSource = dtCustomer_Type;
                Customer_TypeStore.DataBind();
            }

            var clsEnterprise = new Enterprise_BLL();
            DataTable dtEnterprise = clsEnterprise.BindGirdEnterprise(string.Empty);
            if (dtEnterprise != null && dtEnterprise.Rows.Count > 0)
            {
                EnterpriseStore.DataSource = dtEnterprise;
                EnterpriseStore.DataBind();
            }

            var clsSector = new Sector_BLL();
            DataTable dtSector = clsSector.BindGirdSector(string.Empty);
            if (dtSector != null && dtSector.Rows.Count > 0)
            {
                SectorStore.DataSource = dtSector;
                SectorStore.DataBind();
            }

            var clsBank = new Bank_BLL();
            DataTable dtBank = clsBank.BindGirdtb_Bank(string.Empty);
            if (dtBank != null && dtBank.Rows.Count > 0)
            {
                BankStore.DataSource = dtBank;
                BankStore.DataBind();
            }

            var clsProvince = new Province_BLL();
            DataTable dtProvince = clsProvince.BindGirdProvince(string.Empty);
            if (dtProvince != null && dtProvince.Rows.Count > 0)
            {
                ProvinceStore.DataSource = dtProvince;
                ProvinceStore.DataBind();
            }

            var clsDistricts = new Districts_BLL();
            DataTable dtDistricts = clsDistricts.BindGirdDistricts(string.Empty);
            if (dtDistricts != null && dtDistricts.Rows.Count > 0)
            {
                DistrictsStore.DataSource = dtDistricts;
                DistrictsStore.DataBind();
            }

            var clsDistricts1 = new Districts_BLL();
            DataTable dtDistricts1 = clsDistricts1.BindGirdDistricts(string.Empty);
            if (dtDistricts1 != null && dtDistricts1.Rows.Count > 0)
            {
                Districts1Store.DataSource = dtDistricts1;
                Districts1Store.DataBind();
            }
        }
        protected void ProvinceSelectIndex_Change(object sender, StoreReadDataEventArgs e)
        {
            cbDistricts.ReadOnly = false;
            var clsDistricts = new Districts_BLL();
            if (cbProvince.SelectedItem.Value != null)
            {
                cbDistricts1.Clear();
                var dtDistricts = clsDistricts.BindGirdDistricts_Province(int.Parse(cbProvince.SelectedItem.Value));
                if (dtDistricts != null && dtDistricts.Rows.Count > 0)
                {
                    DistrictsStore.DataSource = dtDistricts;
                    DistrictsStore.DataBind();
                }
                else
                {
                    cbProvince.Clear();
                    cbDistricts.ReadOnly = true;
                    HTML.clsFormat.Alert_INFO("Thông báo", "Tỉnh này chưa có quận huyện vui lòng chọn tỉnh khác !");
                }

                cbProvince1.SetValue(int.Parse(cbProvince.SelectedItem.Value));
            }
            else
            {
                cbProvince.Clear();
                cbDistricts.ReadOnly = true;
                HTML.clsFormat.Alert_INFO("Thông báo", "Vui lòng chọn tỉnh !");
            }
        }
        protected void DistrictSelectIndex_Change(object sender, DirectEventArgs e)
        {
            var clsDistricts1 = new Districts_BLL();
            DataTable dtDistricts1 = clsDistricts1.BindGirdDistricts(string.Empty);
            if (dtDistricts1 != null && dtDistricts1.Rows.Count > 0)
            {
                Districts1Store.DataSource = dtDistricts1;
                Districts1Store.DataBind();
            }
            cbDistricts1.SetValue(int.Parse(cbDistricts.SelectedItem.Value));
        }
        protected void Province1SelectIndex_Change(object sender, StoreReadDataEventArgs e)
        {
            cbDistricts1.ReadOnly = false;
            var clsDistricts1 = new Districts_BLL();
            if (cbProvince1.SelectedItem.Value != null)
            {
                var dtDistricts1 = clsDistricts1.BindGirdDistricts_Province(int.Parse(cbProvince1.SelectedItem.Value));
                if (dtDistricts1 != null && dtDistricts1.Rows.Count > 0)
                {
                    Districts1Store.DataSource = dtDistricts1;
                    Districts1Store.DataBind();
                }
                else
                {
                    cbProvince1.Clear();
                    cbDistricts1.ReadOnly = true;
                    HTML.clsFormat.Alert_INFO("Thông báo", "Tỉnh này chưa có quận huyện vui lòng chọn tỉnh khác !");
                }
            }
            else
            {
                cbProvince1.Clear();
                cbDistricts1.ReadOnly = true;
                HTML.clsFormat.Alert_INFO("Thông báo", "Vui lòng chọn tỉnh !");
            }
        }
        protected void ReloadComboBox(object sender, StoreReadDataEventArgs e)
        {
            GetComboBox();
        }
        #endregion
        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            FormPanelCustomer.Reset();

            txtCustomer_ID.Text = string.Empty;
            txtCustomer_Alias.Text = MaTuDong();
            txtCustomer_Name.Text = string.Empty;
            txtCustomer_Name_EN.Text = string.Empty;
            txtCustomer_Name_Exchange.Text = string.Empty;
            txtCustomer_Shortcut.Text = string.Empty;
            txtCustomer_Address.Text = string.Empty;
            txtCustomer_Debt_Limit.Text = "0";
            txtCustomer_Payment_Deadline.Text = "0";
            txtCustomer_Establish.Text = string.Empty;
            txtCustomer_Phone1.Text = string.Empty;
            txtCustomer_Phone2.Text = string.Empty;
            txtCustomer_Fax.Text = string.Empty;
            txtCustomer_Website.Text = string.Empty;
            txtCustomer_Account_Number.Text = string.Empty;
            txtCustomer_Contact_Job.Text = string.Empty;
            txtCustomer_Contact_Phone.Text = string.Empty;
            txtCustomer_Contact_MobilePhone.Text = string.Empty;
            txtCustomer_Contact_Email.Text = string.Empty;
            txtCustomer_Contract_FullName.Text = string.Empty;
            txtCustomer_Contract_Address.Text = string.Empty;
            txtCustomer_Contract_Tax_Code.Text = string.Empty;
            txtCustomer_Contract_Contact_Job.Text = string.Empty;
            txtCustomer_Contract_Contact_Phone.Text = string.Empty;
            txtCustomer_Contract_Contact_MobilePhone.Text = string.Empty;
            txtCustomer_Contract_Contact_Email.Text = string.Empty;
            txtCustomer_Delivery_Address.Text = string.Empty;
            txtCustomer_Delivery_Contact_Job.Text = string.Empty;
            txtCustomer_Delivery_Contact_Phone.Text = string.Empty;
            txtCustomer_Delivery_Contact_MobilePhone.Text = string.Empty;
            txtCustomer_Delivery_Contact_Email.Text = string.Empty;
            txtCustomer_Payment_Address.Text = string.Empty;
            txtCustomer_Payment_Contact_Job.Text = string.Empty;
            txtCustomer_Payment_Contact_Phone.Text = string.Empty;
            txtCustomer_Payment_Contact_MobilePhone.Text = string.Empty;
            txtCustomer_Payment_Contact_Email.Text = string.Empty;
            txtCustomer_Note.Text = string.Empty;

            cbCustomer_Type.Clear();
            cbEnterprise.Clear();
            cbProvince.Clear();
            cbDistricts.Clear(); cbDistricts.ReadOnly = true;
            cbSector.Clear();
            cbBank.Clear();
            cbProvince1.Clear();
            cbDistricts1.Clear();
        }
        #endregion
        #region[MaTangTuDong]
        protected string MaTuDong()
        {
            int i;
            DataTable dt = clsCustomer.BindGirdtb_Customer(string.Empty);
            if (dt != null && dt.Rows.Count > 0)
            {
                int max = int.Parse(dt.Rows[0]["Customer_ID"].ToString());
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    if (int.Parse(dt.Rows[i]["Customer_ID"].ToString()) > max)
                    {
                        max = int.Parse(dt.Rows[i]["Customer_ID"].ToString());
                    }
                }
                i = max + 1;
            }
            else
            {
                i = 0;
            }
            return "KH_00" + i;
        }
        #endregion
        #region[Search]
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            DataTable dtCustomer = clsCustomer.BindGirdtb_Customer(txtSearch.Text);
            if (dtCustomer != null && dtCustomer.Rows.Count > 0)
            {
                StoreCustomer.DataSource = dtCustomer;
                StoreCustomer.DataBind();
            }
        }
        #endregion
        #region[CustomerContact]
        protected void btnCustomer_Contact_Click(object sender, DirectEventArgs e)
        {
            GetCustomerContact();
            WindowCustomerContact.Show();
        }
        protected void btnCustomer_Contract_Click(object sender, DirectEventArgs e)
        {
            GetCustomerContract();
            WindowCustomerContract.Show();
        }

        protected void btnCustomer_Delivery_Click(object sender, DirectEventArgs e)
        {
            GetCustomerDelivery();
            WindowCustomerDelivery.Show();
        }

        protected void btnCustomer_Payment_Click(object sender, DirectEventArgs e)
        {
            GetCustomerPayment();
            WindowCustomerPayment.Show();
        }
        protected void GetCustomerContact()
        {
            var clsCustomerContact = new Contact_BLL();
            DataTable dtCustomerContact = clsCustomerContact.BindGirdContact(txtSearchCustomerContact.Text);
            if (dtCustomerContact != null && dtCustomerContact.Rows.Count > 0)
            {
                StoreCustomerContact.DataSource = dtCustomerContact;
                StoreCustomerContact.DataBind();
            }
            else
            {
                grCustomerContact.GetStore().RemoveAll();
            }
        }
        protected void GetCustomerContract()
        {
            var clsCustomerContract = new Contact_BLL();
            DataTable dtCustomerContract = clsCustomerContract.BindGirdContact(txtSearchCustomerContract.Text);
            if (dtCustomerContract != null && dtCustomerContract.Rows.Count > 0)
            {
                StoreCustomerContract.DataSource = dtCustomerContract;
                StoreCustomerContract.DataBind();
            }
            else
            {
                grCustomerContact.GetStore().RemoveAll();
            }
        }
        protected void GetCustomerDelivery()
        {
            var clsCustomerDelivery = new Contact_BLL();
            DataTable dtCustomerDelivery = clsCustomerDelivery.BindGirdContact(txtSearchCustomerDelivery.Text);
            if (dtCustomerDelivery != null && dtCustomerDelivery.Rows.Count > 0)
            {
                StoreCustomerDelivery.DataSource = dtCustomerDelivery;
                StoreCustomerDelivery.DataBind();
            }
            else
            {
                grCustomerContact.GetStore().RemoveAll();
            }
        }
        protected void GetCustomerPayment()
        {
            var clsCustomerPayment = new Contact_BLL();
            DataTable dtCustomerPayment = clsCustomerPayment.BindGirdContact(txtSearchCustomerPayment.Text);
            if (dtCustomerPayment != null && dtCustomerPayment.Rows.Count > 0)
            {
                StoreCustomerPayment.DataSource = dtCustomerPayment;
                StoreCustomerPayment.DataBind();
            }
            else
            {
                grCustomerContact.GetStore().RemoveAll();
            }
        }
        protected void StoreCustomerContact_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            StoreCustomerContact.Reload();
        }
        protected void StoreCustomerContract_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            StoreCustomerContract.Reload();
        }
        protected void StoreCustomerDelivery_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            StoreCustomerDelivery.Reload();
        }
        protected void StoreCustomerPayment_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            StoreCustomerPayment.Reload();
        }
        protected void CellCustomerContact_Click(object sender, DirectEventArgs e)
        {
            var sm = grCustomerContact.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                string id = sm.SelectedCell.RecordID;
                var clsCustomerContact = new Contact_BLL();
                DataTable dtCustomerContact = clsCustomerContact.BindGirdContactDetail(int.Parse(id));
                txtCustomer_Contact_ID.Text = dtCustomerContact.Rows[0]["Contact_ID"].ToString();
                txtCustomer_Contact_Name.Text = dtCustomerContact.Rows[0]["Contact_Name"].ToString();
                txtCustomer_Contact_Job.Text = dtCustomerContact.Rows[0]["Job_Name"].ToString();
                txtCustomer_Contact_Email.Text = dtCustomerContact.Rows[0]["Contact_Email"].ToString();
                txtCustomer_Contact_Phone.Text = dtCustomerContact.Rows[0]["Contact_Phone"].ToString();
                txtCustomer_Contact_MobilePhone.Text = dtCustomerContact.Rows[0]["Contact_MobilePhone"].ToString();
                sm.Clear();
                sm.UpdateSelection();
                WindowCustomerContact.Close();
            }
        }
        protected void CellCustomerContract_Click(object sender, DirectEventArgs e)
        {
            var sm = grCustomerContract.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                string id = sm.SelectedCell.RecordID;
                var clsCustomerContract = new Contact_BLL();
                DataTable dtCustomerContract = clsCustomerContract.BindGirdContactDetail(int.Parse(id));
                txtCustomer_Contract_Contact_ID.Text = dtCustomerContract.Rows[0]["Contact_ID"].ToString();
                txtCustomer_Contract_Contact_Name.Text = dtCustomerContract.Rows[0]["Contact_Name"].ToString();
                txtCustomer_Contract_Contact_Job.Text = dtCustomerContract.Rows[0]["Job_Name"].ToString();
                txtCustomer_Contract_Contact_Email.Text = dtCustomerContract.Rows[0]["Contact_Email"].ToString();
                txtCustomer_Contract_Contact_Phone.Text = dtCustomerContract.Rows[0]["Contact_Phone"].ToString();
                txtCustomer_Contract_Contact_MobilePhone.Text = dtCustomerContract.Rows[0]["Contact_MobilePhone"].ToString();
                sm.Clear();
                sm.UpdateSelection();
                WindowCustomerContract.Close();
            }
        }
        protected void CellCustomerDelivery_Click(object sender, DirectEventArgs e)
        {
            var sm = grCustomerDelivery.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                string id = sm.SelectedCell.RecordID;
                var clsCustomerDelivery = new Contact_BLL();
                DataTable dtCustomerDelivery = clsCustomerDelivery.BindGirdContactDetail(int.Parse(id));
                txtCustomer_Delivery_Contact_ID.Text = dtCustomerDelivery.Rows[0]["Contact_ID"].ToString();
                txtCustomer_Delivery_Contact_Name.Text = dtCustomerDelivery.Rows[0]["Contact_Name"].ToString();
                txtCustomer_Delivery_Contact_Job.Text = dtCustomerDelivery.Rows[0]["Job_Name"].ToString();
                txtCustomer_Delivery_Contact_Email.Text = dtCustomerDelivery.Rows[0]["Contact_Email"].ToString();
                txtCustomer_Delivery_Contact_Phone.Text = dtCustomerDelivery.Rows[0]["Contact_Phone"].ToString();
                txtCustomer_Delivery_Contact_MobilePhone.Text = dtCustomerDelivery.Rows[0]["Contact_MobilePhone"].ToString();
                sm.Clear();
                sm.UpdateSelection();
                WindowCustomerDelivery.Close();
            }
        }
        protected void CellCustomerPayment_Click(object sender, DirectEventArgs e)
        {
            var sm = grCustomerPayment.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                string id = sm.SelectedCell.RecordID;
                var clsCustomerPayment = new Contact_BLL();
                DataTable dtCustomerPayment = clsCustomerPayment.BindGirdContactDetail(int.Parse(id));
                txtCustomer_Payment_Contact_ID.Text = dtCustomerPayment.Rows[0]["Contact_ID"].ToString();
                txtCustomer_Payment_Contact_Name.Text = dtCustomerPayment.Rows[0]["Contact_Name"].ToString();
                txtCustomer_Payment_Contact_Job.Text = dtCustomerPayment.Rows[0]["Job_Name"].ToString();
                txtCustomer_Payment_Contact_Email.Text = dtCustomerPayment.Rows[0]["Contact_Email"].ToString();
                txtCustomer_Payment_Contact_Phone.Text = dtCustomerPayment.Rows[0]["Contact_Phone"].ToString();
                txtCustomer_Payment_Contact_MobilePhone.Text = dtCustomerPayment.Rows[0]["Contact_MobilePhone"].ToString();
                sm.Clear();
                sm.UpdateSelection();
                WindowCustomerPayment.Close();
            }
        }
        protected void SearchCustomerContact_Event(object sender, DirectEventArgs e)
        {
            GetCustomerContact();
        }
        protected void SearchCustomerContract_Event(object sender, DirectEventArgs e)
        {
            GetCustomerContract();
        }
        protected void SearchCustomerDelivery_Event(object sender, DirectEventArgs e)
        {
            GetCustomerDelivery();
        }
        protected void SearchCustomerPayment_Event(object sender, DirectEventArgs e)
        {
            GetCustomerPayment();
        }
        #endregion
        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Customer, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                btnAddNew.Visible = Is_BtnAdd;
                btnEdit.Visible = Is_BtnEdit;
                PanelCustomer.Visible = Is_BtnView;
                btnDel.Visible = Is_BtnDel;
                btnExport.Visible = Is_BtnPrint;
            }
        }
        private bool CheckPermisstionDobclick()
        {
            var check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Customer, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion

        private void WindowsCustomer_Type()
        {
            Window win = new Window()
            {
                ID = "WindowsCustomer_Type",
                Title = "Loại khách hàng",
                Width = 829,
                Height = 400,
                Modal = true,
                AutoRender = false,
                Collapsible = false,
                Maximizable = true,
                Hidden = true,
                Loader = new ComponentLoader
                {
                    Url = "../PageParrent/Customer_Type.aspx",
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true
                    }
                }
            };
            this.Controls.Add(win);
        }
        private void WindowsSector()
        {
            Window win = new Window()
            {
                ID = "WindowsSector",
                Title = "Ngành nghề",
                Width = 829,
                Height = 400,
                Modal = true,
                AutoRender = false,
                Collapsible = false,
                Maximizable = true,
                Hidden = true,
                Loader = new ComponentLoader
                {
                    Url = "../PageParrent/Sector.aspx",
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true
                    }
                }
            };
            this.Controls.Add(win);
        }
        private void WindowsEnterprise()
        {
            Window win = new Window()
            {
                ID = "WindowsEnterprise",
                Title = "Loại hình doanh nghiệp",
                Width = 829,
                Height = 400,
                Modal = true,
                AutoRender = false,
                Collapsible = false,
                Maximizable = true,
                Hidden = true,
                Loader = new ComponentLoader
                {
                    Url = "../PageParrent/Enterprise.aspx",
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true
                    }
                }
            };
            this.Controls.Add(win);
        }
        private void WindowsProvince()
        {
            Window win = new Window()
            {
                ID = "WindowsProvince",
                Title = "Tỉnh",
                Width = 829,
                Height = 400,
                Modal = true,
                AutoRender = false,
                Collapsible = false,
                Maximizable = true,
                Hidden = true,
                Loader = new ComponentLoader
                {
                    Url = "../PageParrent/Province.aspx",
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true
                    }
                }
            };
            this.Controls.Add(win);
        }
        private void WindowsDistricts()
        {
            Window win = new Window()
            {
                ID = "WindowsDistricts",
                Title = "Quận huyện",
                Width = 829,
                Height = 400,
                Modal = true,
                AutoRender = false,
                Collapsible = false,
                Maximizable = true,
                Hidden = true,
                Loader = new ComponentLoader
                {
                    Url = "../PageParrent/Districts.aspx",
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true
                    }
                }
            };
            this.Controls.Add(win);
        }
        private void WindowsBank()
        {
            Window win = new Window()
            {
                ID = "WindowsBank",
                Title = "Ngân hàng",
                Width = 829,
                Height = 400,
                Modal = true,
                AutoRender = false,
                Collapsible = false,
                Maximizable = true,
                Hidden = true,
                Loader = new ComponentLoader
                {
                    Url = "../PageParrent/Bank.aspx",
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true
                    }
                }
            };
            this.Controls.Add(win);
        }
    }
}